<?php

namespace App\Admin\Controller\Cms;

class Article extends \App\Admin\Controller\Authed {

    public function index() {

        $do = $this->request->get('do');

        if ($do == 'top_sort') {
            $tops = $this->request->getPost('tops');

            foreach ($tops as $key => $value) {
                $result = Cms()->Article()->setTop($key, $value);

                if (!$result) {
                    fail();
                }
            }

            succ();
        }

        if ($do == 'hot_sort') {
            $hots = $this->request->getPost('hots');

            foreach ($hots as $key => $value) {
                $result = Cms()->Article()->setHot($key, $value);

                if (!$result) {
                    fail();
                }
            }

            succ();
        }
        
        if ($do == 'deleteall') {

            $ids = $this->request->get('ids');
            
            $result = Cms()->Article()->removeAll($ids);

            if ($result) {
                $this->succ();
            } else {
                $this->fail();
            }
            
        }

        $filter_title = urldecode(trim($this->request->get('filter_title')));
        $filter_top = intval($this->request->get('filter_top'));
        $filter_hot = intval($this->request->get('filter_hot'));
        $filter_begin = trim($this->request->get('filter_begin', ''));
        $filter_end = trim($this->request->get('filter_end', ''));
        $filter_column_id = $this->request->get('filter_column_id');

        if ($filter_column_id) {
            $filter['column_id'] = $filter_column_id;
        }

        if ($filter_top == 1) {
            $filter['top'] = array('>' => 0);
        }

        if ($filter_top == -1) {
            $filter['top'] = 0;
        }

        if ($filter_hot == 1) {
            $filter['hot'] = array('>' => 0);
        }

        if ($filter_hot == -1) {
            $filter['hot'] = 0;
        }

        if ($filter_title) {
            $filter['title'] = array('like' => "%{$filter_title}%");
        }

        if ($filter_begin && $filter_end) {
            $filter['add_time'] = array('between' => $filter_begin, 'and' => $filter_end . ' 23:59:59');
        }

        $count = Cms()->Article()->countAll($filter);

        /* 分页 */
        $pager = $this->_createPager($count);
        $length = $pager['length'];
        $start = $pager['offset'];
        $this->assign('pager', $pager);

        $rows = Cms()->Article()->getAll($start, $length, $filter);

        foreach ($rows as &$row) {
            $row['column_title'] = Cms()->Column()->get($row['column_id'], 'title');
            $row['type_title'] = Cms()->Article()->getTypeAssoc($row['type']);
            $row['state_title'] = Cms()->Article()->getStateAssoc($row['state']);
        }

        $this->rows = $rows;

        $this->assign('columns', Cms()->Column()->getTreeArray());
    }

    public function edit() {
        
        $this->enablePageCache(true);

        $id = intval($this->request->get('id'));

        $editor = new \WangEditor4();
        $editor->chunk_upload_server = '/admin/upload/chunk';
        $editor->base64_upload_server = '/admin/upload/base64';
        $editor->remote_capture_server = '/admin/upload/capture';

        if ($this->request->isPost()) {

            $data = $this->request->get();

            if (!$data['column_id']) {
                fail('请选择栏目');
            }

            $data['jump'] = intval($this->request->get('jump'));

            $data['content'] = $editor->getContent();

            if (!$data['column_id']) {
                fail('请选择栏目');
            }

            if (!$data['title']) {
                fail('标题不能为空');
            }

            if ($id) {
                $data['update_time'] = date('Y-m-d H:i:s');
                $result = Cms()->Article()->update($id, $data);
                if ($result) {

                    // 更新标签
                    $result = Cms()->Article()->Tag()->clearByArticle($id);
                    if (!$result) {
                        fail('标签操作失败');
                    }

                    if ($data['tag_ids']) {
                        foreach ($data['tag_ids'] as $tag_id) {
                            $result = Cms()->Article()->Tag()->add(['article_id' => $id, 'tag_id' => $tag_id]);
                            if (!$result) {
                                fail('标签操作失败');
                            }
                        }
                    }

                    succ('', '', 2);
                } else {
                    fail();
                }
            } else {
                
                if (!$data['publish_time']) {
                    $data['publish_time'] = date('Y-m-d H:i:s');
                }
                
                $id = Cms()->Article()->add($data);
                if ($id) {

                    // 更新标签
                    $result = Cms()->Article()->Tag()->clearByArticle($id);
                    if (!$result) {
                        fail('标签操作失败');
                    }

                    if ($data['tag_ids']) {
                        foreach ($data['tag_ids'] as $tag_id) {
                            $result = Cms()->Article()->Tag()->add(['article_id' => $id, 'tag_id' => $tag_id]);
                            if (!$result) {
                                fail('标签操作失败');
                            }
                        }
                    }

                    succ('', '', 2);
                } else {
                    fail();
                }
            }
        }

        $this->detail = Cms()->Article()->get($id);

        $this->columns = Cms()->Column()->getTreeArray();

        $editor->setContent($this->detail['content']);

        $this->editor = $editor;

        $this->types = Cms()->Article()->getTypeAssoc();

        $this->states = Cms()->Article()->getStateAssoc();

        $tags = Cms()->Tag()->getList();

        if ($id) {
            foreach ($tags as &$tag) {
                $tag['checked'] = Cms()->Article()->Tag()->existByArticleTag($id, $tag['id']);
            }
            unset($tag);
        }

        $this->tags = $tags;
    }

    public function remove() {

        $id = intval($this->request->get('id'));

        $result = Cms()->Article()->remove($id);

        if ($result) {
            succ();
        } else {
            fail();
        }
    }

}
